﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using HtmlAgilityPack;

namespace OlapProject
{
    public partial class Form1 : Form
    {
        private ParseManager parseEngine = new ParseManager();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < parsersList.Items.Count; i++)
            {
                parsersList.SetSelected(i, true);
                parsersList.SetItemCheckState(i,CheckState.Checked);
            }
            Log.init(consoleBox);
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            pivotGrid.FieldsCustomization(olapSplitPanel.Panel1);
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Log.writeLine("PARSING DATA COMPLETED!!!", Color.Green);
        }

        private void debugModeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            DebugModel.debugMode = !DebugModel.debugMode;
        }

        private void parseButton_Click(object sender, EventArgs e)
        {
            if (worker.IsBusy)
            {
                Log.writeLine("I'M WORKING !!!!!", Color.Red);         
                consoleBox.Refresh();
                return;
            }

            parseEngine.reset();
            setupChosenParsers();
            startParse();
       
        }

        private void setupChosenParsers()
        {

            for (int i = 0; i < parsersList.Items.Count; i++)
            {
                if (parsersList.GetItemCheckState(i) == CheckState.Checked)
                {
                    parseEngine.useParser(i);
                }
            }
        }

        private void startParse()
        {

            worker.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs args)
            {
                parseEngine.run();

            });

            worker.RunWorkerAsync();
        }

        private void consoleBox_TextChanged(object sender, EventArgs e)
        {
            consoleBox.SelectionStart = consoleBox.Text.Length; 
            consoleBox.ScrollToCaret(); 
        }
  
    }
}
